$(function () {
    obj.init();
});
var obj = new function () {
    return {
        dataGrid: null,
        init: function () {
            this.dataGrid = new xhDataGrid($('#list'), 'device/getDeviceSignIn');
            this.dataGrid.parseToolbar().init().activeButton([], ['bt_list_changeStatus']);
            xhExtra.clearLoadSuccess(this.dataGrid);
            this.listener();
        },
        listener: function () {
            var $this = this;
            //查询
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function () {
                    $this.funObj().searchList();
                })
            );
            //双击
            $this.dataGrid.onDblClickRow = function(rowIndex, rowData){
                var id = rowData['iId'];
            };
            //查看签名图片
            $('.xh-datagrid').on('click','.image',function () {
                var imageUrl = $(this).attr('imageUrl');
                window.top.xhTop.showModal({
                    width:'800',
                    title:'查看签名',
                    content: '<img style="max-width:100%" src="'+imageUrl+'" alt="无签名数据">'
                });
            });
            //修改状态
            $('#bt_list_changeStatus').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().changeStatus();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                searchList: function () {
                    $this.dataGrid.reload({
                        search: {
                            vague: xhExtra.getValue($('#list_vague'))
                        },
                        status: xhExtra.getValue($('#device_signIn'))
                    })
                },
                changeStatus: function () {
                    var id = $this.dataGrid.getSelectValue('iId');
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '将状态修改为：',
                        html: '<div>' +
                        '<select id="status" class="swal2-input">' +
                        '<option value="0">未签收</option>' +
                        '<option value="1">已签收</option>' +
                        '</select>' +
                        '</div>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var status = $('#status', window.top.document).val();
                                xhExtra.ajaxRequest('Device/modifySignStatus', {
                                    id: id,
                                    statusCode: status
                                }, {success: true});
                                resolve();
                                $this.dataGrid.reload();
                            })
                        }
                    })
                }
            }
        }
    }
};